package com.ruoyi.model.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.model.domain.ModelTrainResult;
import com.ruoyi.model.service.IModelResultService;
//import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.model.domain.ModelData;
import com.ruoyi.model.service.IModelDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模型Controller
 * 
 * @author weihanqiang
 * @date 2025-04-30
 */
@RestController
@RequestMapping("/model/data")
public class ModelDataController extends BaseController
{
    @Autowired
    private IModelDataService modelDataService;

    @Autowired
    private IModelResultService modelResultService;


    /**
     * 查询模型列表
     */
    @PreAuthorize("@ss.hasPermi('model:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(ModelData modelData)
    {
        startPage();
        List<ModelData> list = modelDataService.selectModelDataList(modelData);
        return getDataTable(list);
    }

    /**
     * 导出模型列表
     */
    @PreAuthorize("@ss.hasPermi('model:data:export')")
    @Log(title = "模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModelData modelData)
    {
        List<ModelData> list = modelDataService.selectModelDataList(modelData);
        ExcelUtil<ModelData> util = new ExcelUtil<ModelData>(ModelData.class);
        util.exportExcel(response, list, "模型数据");
    }

    /**
     * 获取模型详细信息
     */
    @PreAuthorize("@ss.hasPermi('model:data:query')")
    @GetMapping(value = "/{modelId}")
    public AjaxResult getInfo(@PathVariable("modelId") Long modelId)
    {
        return success(modelDataService.selectModelDataByModelId(modelId));
    }

    /**
     * 新增模型
     */
    @PreAuthorize("@ss.hasPermi('model:data:add')")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ModelData modelData)
    {
        modelData.setCreatorId(getUserId());
        modelData.setCreator(getUsername());
        modelData.setTrainerId(getUserId());
        modelData.setTrainerName(getUsername());
        return toAjax(modelDataService.insertModelData(modelData));
    }

    /**
     * 修改模型
     */
    @PreAuthorize("@ss.hasPermi('model:data:edit')")
    @Log(title = "模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ModelData modelData)
    {
        return toAjax(modelDataService.updateModelData(modelData));
    }

    /**
     * 删除模型
     */
    @PreAuthorize("@ss.hasPermi('model:data:remove')")
    @Log(title = "模型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{modelIds}")
    public AjaxResult remove(@PathVariable Long[] modelIds)
    {
        return toAjax(modelDataService.deleteModelDataByModelIds(modelIds));
    }

    /**
     * 新增模型训练详情
     */
    @PreAuthorize("@ss.hasPermi('model:data:train')")
    @Log(title = "模型明细", businessType = BusinessType.INSERT)
    @PostMapping("/train")
    public AjaxResult addModelTrainDetail(@RequestBody List<ModelTrainResult> modelList )
    {
        // 调用函数插入modelTrainResult到redis
        try {
            // 调用缓存方法存储数据到 Redis
            for (ModelTrainResult model : modelList) {
                modelResultService.cacheModelResult(model);
            }
            return AjaxResult.success("模型结果已缓存成功");
        } catch (Exception e) {
            return AjaxResult.error("缓存模型结果失败: " + e.getMessage());
        }
    }

    /**
     * 获取模型详细信息
     */
    @PreAuthorize("@ss.hasPermi('model:data:train')")
    @GetMapping(value = "/train/{modelNo}")
    public AjaxResult getModelTrainDetail(@PathVariable("modelNo") String modelNo)
    {
        return success(modelResultService.getCachedModelResults(modelNo));
    }

    /**
     * 删除模型训练详情
     */
    @PreAuthorize("@ss.hasPermi('model:data:train')")
    @Log(title = "模型明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/train/{modelNo}")
    public AjaxResult addModelTrainDetail(@PathVariable String modelNo)
    {
        try {
            modelResultService.deleteCachedModelResults(modelNo);
            return AjaxResult.success("模型结果已成功删除");
        } catch (Exception e) {
            return AjaxResult.error("模型结果删除失败: " + e.getMessage());
        }
    }
}
